package cn.nosql.tools;

import org.h2.mvstore.MVMap;
import org.h2.mvstore.MVStore;
import org.h2.store.fs.FileUtils;
import org.junit.Test;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

public class MyConvertUtilCase {

    @Test
    public void my_data_1()
    {
        byte[] bs = MyConvertUtil.objToBytes("abc");
        ByteBuffer byteBuffer = ByteBuffer.wrap(bs);

        byte[] bytes = new byte[byteBuffer.capacity()];
        byteBuffer.get(bytes, 0, bytes.length);

        String line = MyConvertUtil.restore(bytes).toString();
        //String line = MyConvertUtil.restore(bs).toString();
        System.out.println(line);
    }

    @Test
    public void my_data()
    {
        //ByteBuffer bv = MyConvertUtil.toByteBuffer(123456);
        ByteBuffer bv = MyConvertUtil.toByteBuffer("abc");
        System.out.println(MyConvertUtil.toObj(bv));
    }

    @Test
    public void put_data()
    {
        String fileName = "/Users/chenfei/temp/my_store.db";
        //FileUtils.delete(fileName);

        MVStore.Builder builder = new MVStore.Builder();
        builder.fileName(fileName);
        MVStore store = builder.open();

        MVMap<Integer, String> m = store.openMap("cache_data");
        ByteBuffer bi = MyConvertUtil.toByteBuffer(2);
        ByteBuffer bv = MyConvertUtil.toByteBuffer("吴大富");

        Integer key = (Integer) MyConvertUtil.toObj(bi);
        String vs = (String) MyConvertUtil.toObj(bv);

        System.out.println(key);
        System.out.println(vs);
        m.put(key, vs);

        store.commit();
        store.close();
    }

    @Test
    public void get_data()
    {
        String fileName = "/Users/chenfei/temp/my_store.db";
        //FileUtils.delete(fileName);

        MVStore.Builder builder = new MVStore.Builder();
        builder.fileName(fileName);
        MVStore store = builder.open();

        MVMap<Integer, String> m = store.openMap("cache_data");
        ByteBuffer bi = MyConvertUtil.toByteBuffer(2);
        String vs = m.get((Integer) MyConvertUtil.toObj(bi));
        System.out.println(vs);
    }

    @Test
    public void my_test()
    {
        String path = MyConvertUtilCase.class.getProtectionDomain().getCodeSource().getLocation().toString();
        System.out.println(path);
    }

    @Test
    public void convert_1()
    {
        List<String> lst = new ArrayList<>();
        lst.add("吴大富");
        lst.add("吴大贵");
        lst.add("美羊羊");

        List<Byte> lst_byte = MyConvertUtil.objToListBytes(lst);
        Object lst_1 = MyConvertUtil.restoreToObject(lst_byte);
        List<String> lst_2 = (List<String>) lst_1;
        for (String s : lst_2)
        {
            System.out.println(s);
        }
    }
}














































